﻿using System;
using System.Web.UI.WebControls;
using eCommerce.DataAccess;

namespace eCommerce.CMS.CustomAdmin.Controls.Pages
{
    public partial class Add : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                WebsiteDataContext db = new WebsiteDataContext();

                var query = db.getParent();

                ddlParent.DataSource = query;
                ddlParent.DataTextField = "pagetitle";
                ddlParent.DataValueField = "pageid";
                ddlParent.DataBind();
                ddlParent.Items.Insert(0, new ListItem("None", "0"));

                ddlExternal.SelectedValue = "False";
                tbxPosition.Text = "0";
                ddlUseCont.SelectedValue = "True";
                ddlSuppressNavi.SelectedValue = "False";
                ddlSuppressHotT.SelectedValue = "False";
                tbxUseColl.Text = "0";
                ddlSecured.SelectedValue = "False";
                ddlNewWind.SelectedValue = "False";

                UpdateDateTbx.Text = DateTime.Now.ToShortDateString();

            }
        }

        protected void AddBtn_Click(object sender, EventArgs e)
        {

            using (WebsiteDataContext ndc = new WebsiteDataContext())
            {

                Xcms_Page pag = new Xcms_Page();
                pag.pageupid = Convert.ToInt32((string) ddlParent.SelectedValue.ToString());
                pag.pagemarker = tbxMarker.Text;
                pag.pagetitle = tbxTitle.Text;
                pag.pagenavigation = tbxNavi.Text;
                pag.pagecontent = FCKText.Value;
                pag.pagemetatags = tbxMetaKey.Text;
                pag.pagemetadescription = tbxMetaDesc.Text;
                pag.pagespecial = tbxSpecial.Text;
                //pag.externallink = Convert.ToBoolean(tbxExternal.Text);
                pag.pageorder = Convert.ToInt32((string) tbxPosition.Text);
                pag.pageusecontent = Convert.ToBoolean((string) ddlUseCont.SelectedValue);
                pag.pagesuppressnavigation = Convert.ToBoolean((string) ddlSuppressNavi.SelectedValue);
                pag.pagesuppresshottext = Convert.ToBoolean((string) ddlSuppressHotT.SelectedValue);
                pag.pageusecollection = Convert.ToInt32((string) tbxUseColl.Text);
                pag.pagerestrict = Convert.ToBoolean((string) ddlSecured.SelectedValue);
                pag.pagenewwindow = Convert.ToBoolean((string) ddlNewWind.SelectedValue);
                //pag.updatedate = Convert.ToDateTime(UpdateDateTbx.Text);

                ndc.Xcms_Pages.InsertOnSubmit(pag);
                ndc.SubmitChanges();

                if (pag.pageid == 0)
                {
                    lblTest.Text = "Error";
                }
                else
                {
                    lblTest.Text = "A new page has been added";
                }

                Response.Redirect("Pages.aspx?action=List");

            }

        }
        protected void lnkbtnDev_Click(object sender, EventArgs e)
        {
            pnlDevelop.Visible = true;
        }

        protected void lnkbtnUnDev_Click(object sender, EventArgs e)
        {
            pnlDevelop.Visible = false;
        }
    }
}